using UnityEngine;
using System.Collections;

public class moveScript : MonoBehaviour {
	float speed = 10.0f;
	float moveSpeed = 70.0f;
	float jumpGap = 2f;
	int health = 50;
	int itemsCollected = 0;
	int totalObjects = 3;
	float healthInterval = 0.1f;
	public GUIText healthText;
	bool healthIsUp = true;
	bool isUp = true;
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
		healthText.text = "Health: " + health;
		
		if(itemsCollected == totalObjects)
		{
			Application.LoadLevel(1);
		}
		
		if(health <= 0)
		{
			Application.LoadLevel(2);
		}
		if(Input.GetKey("w"))
		{
			//Vector3 pos = transform.position;
			//pos.z += 10;
			//transform.position = pos;
			transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
		}
		if(Input.GetKey("s"))
		{
			//Vector3 pos = transform.position;
			//pos.z -= 10;
			//transform.position = pos;
			transform.Translate(Vector3.back * Time.deltaTime * moveSpeed);
		}
		if(Input.GetKey("a"))
		{
			//Vector3 pos = transform.position;
			//pos.x -= 10;
			//transform.position = pos;
			transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
		}
		if(Input.GetKey("d"))
		{
			//Vector3 pos = transform.position;
			//pos.x += 10;
			//transform.position = pos;
			transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
		}
		if(Input.GetKey("space"))
		{
			if(isUp)
			{
				Vector3 pos = transform.position;
				pos.y += 10;
				transform.position = pos;
				isUp = false;
				Invoke("setJumpTrue", jumpGap);
			}
		}
			transform.Rotate(new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X")*2, 0) * Time.deltaTime * speed);
	}
	void setJumpTrue()
	{
		isUp = true;
	}
	void setHealthTrue()
	{
		healthIsUp = true;
	}
	void OnCollisionStart(Collision collision)
	{
		if(collision.gameObject.CompareTag("Enemy"))
		{
				health -= 1;
				healthIsUp = false;
				Invoke("setHealthTrue", healthInterval);
		}
	}
	void OnCollisionStay(Collision collision)
	{
		if(collision.gameObject.CompareTag("Enemy"))
		{
			if(healthIsUp)
			{
				health -= 1;
				healthIsUp = false;
				Invoke("setHealthTrue", healthInterval);
			}
		}
	}
	
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.CompareTag("collect"))
		{
			Destroy(other.gameObject);
			itemsCollected++;
		}
	}
	
}
